#!/bin/bash
#(c) Copyright 2013 Barry Kauler, bkhome.org
#2013 Licence GPL3 (/usr/share/doc/legal)
#perform background housekeeping.

#exec &>/tmp/${0##*/}.log ; set -x #debug

if ! /usr/local/pup_event/frontend_startup $@ ; then
	exit 1
fi

SAVECNT=0
MOUSECNT=0
MINUTECNT=0
CURPOS1=""

. /etc/rc.d/PUPSTATE

# bash builtins: avoid calling some external apps.. specially sleep
for builtin in sleep basename dirname finfo head ln logname mkdir print printenv realpath rmdir sync uname
do
	if [ -f /usr/lib/bash/${builtin} ] ; then
		enable -f /usr/lib/bash/${builtin} ${builtin}
	fi
done

# upupbb:
# systemd-udevd doesn't seem to detect changes in optical drives..
# must monitor optical drives and generate uevents accordingly..
if [ -L /sbin/udevd ] ; then
	case $(readlink /sbin/udevd) in *"/systemd-udevd") SYSTEMD_UDEVD=1 ;; esac
fi

#===============================================

while [ 1 ]
do

	sleep 6

	PS="`busybox ps`"
	while read l ; do
		case $l in *zzFRONTEND_CHANGE*) continue ;; esac
	done <<< "$PS"

	. /etc/eventmanager #has RAMSAVEINTERVAL, HOTPLUGNOISY, BACKENDON, POWERTIMEOUT

	[ "$RAMSAVEINTERVAL" = "" ] && RAMSAVEINTERVAL=30
	RAMSAVEINTERVAL=$(( $RAMSAVEINTERVAL * 60 )) #convert minutes to seconds.
	[ "$POWERTIMEOUT" = "" ] && POWERTIMEOUT=0
	POWERTIMEOUT=$(( $POWERTIMEOUT * 60 )) #convert minutes to seconds.

	case $PUPMODE in 7|13)
		SAVECNT=$(( $SAVECNT + 6 ))
		if [ $RAMSAVEINTERVAL -ne 0 -a $SAVECNT -gt $RAMSAVEINTERVAL ];then
			touch /tmp/snapmergepuppyrequest #request
			SAVECNT=0
		fi
		#monitor free memory, periodic save of tmpfs top layer...
		if [ -f /tmp/snapmergepuppyrequest ];then #by request.
			snapmergepuppy pup_event
		fi
		;;
	esac

	#----------------------------------------------------------------
	#POWERTIMEOUT
	if [ $POWERTIMEOUT -ne 0 ];then #power-off computer after inactivity.
		MOUSECNT=$(( $MOUSECNT + 6 ))
		MINUTECNT=$(( $MINUTECNT + 6 ))
		if [ $MINUTECNT -gt 59 ];then #come in here every minute...
			MINUTECNT=0
			CURPOS2="`getcurpos`"
			if [ -z "$CURPOS1" ] ; then
				CURPOS1="$CURPOS2"
			fi
			if [ "$CURPOS1" != "$CURPOS2" ];then
				MOUSECNT=0
			fi
			CURPOS1="$CURPOS2"
			[ $MOUSECNT -gt $POWERTIMEOUT ] && wmpoweroff
		fi
	fi
	#----------------------------------------------------------------

	[ "$HOTPLUGON" = "false" ] && continue #see /etc/eventmanager
	[ "$ICONDESK" = "false" ] && continue #see /etc/eventmanager
	[ "$SYSTEMD_UDEVD" ] || continue

	[ ! -e /sys/block/sr0 ] && continue

	#unfortunately, we have to poll at regular intervals to see if a cd is inserted...
	for i in /sys/block/sr*
	do
		DRV_NAME=${i##*/}
		DRV_CATEGORY="optical"
		while read dev etc ; do #only check cd if unmounted.
			[ "$dev" = "/dev/${DRV_NAME}" ] && continue 2
		done < /proc/mounts
		#--
		cddetect_quick -d/dev/${DRV_NAME} >/dev/null 2>&1 #very fast.
		if [ $? -eq 0 ];then #inserted
			if [ ! -d /root/.pup_event/drive_${DRV_NAME} ] ; then
				echo "add" > /sys/block/${DRV_NAME}/uevent
			fi
		else # not inserted
			if [ -d /root/.pup_event/drive_${DRV_NAME} ] ; then
				echo "remove" > /sys/block/${DRV_NAME}/uevent
			fi
		fi
	done

done

### END ###
